#include <stdio.h>
#include <string.h>
#include <stdbool.h>

struct student
{
    int id;
    char name[10];
    int age;
};

// typedef struct student
// {
//     int id;
//     char name[10];
//     int age;
// }Stu;

//类型替换
typedef struct student Stu;

//初始化
void InitStu(struct student *stu,int id,const char *name,int age)
{
    //结构体变量的指针访问内存用->
    stu->id = id;
    strcpy(stu->name,name);
    stu->age = age;
}

//输出
void PrintStu(const struct student *stu)
{
    printf("学生id:%d  姓名:%s  年龄:%d\n",stu->id,stu->name,stu->age);
}

//结构体的嵌套
struct teacher
{
    char name[10];
    int age;
    Stu student[10];
    int count;
};
typedef struct teacher teach;

void InitTeach(teach *t,const char *name,int age)
{
    t->age = age;
    strcpy(t->name,name);

    for(int i = 0; i < 10; i++)
    {
        InitStu(&t->student[i],0,"",0);
    }
    t->count = 0;
}

void AddStudent(teach *t,int id,const char *name,int age)
{
    if(t->count > 10)
    {
        printf("学生满了！\n");
        return;
    }
    InitStu(&t->student[t->count++],id,name,age);
}


void PrintTeach(const teach *t)
{
    printf("老师名字:%s 年龄:%d\n",t->name,t->age);
    for(int i = 0; i < t->count; i++)
    {
        printf("学生信息:");
        PrintStu(&t->student[i]);
    }
}

bool CheckID(Stu *s1,Stu *s2)
{
    if(s1->id > s2->id)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool CheckName(Stu *s1,Stu *s2)
{
    if(strcmp(s1->name , s2->name) > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool CheckAge(Stu *s1,Stu *s2)
{
    if(s1->age > s2->age)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void FastSort(Stu *s,int start,int end,bool (*funcptr)(Stu *,Stu *))
{
    if(start >= end)
    return;
    Stu temp = s[start];
    int left = start;
    int right = end;
    
    while(left < right)
    {
        while(left < right && funcptr(&s[right],&temp) == true)
        {
            right--;
        }
        if(left < right)
        {
            s[left] = s[right];
            left++;
        }
        while(left < right && funcptr(&s[left],&temp) == false)
        {
            left++;
        }
        if(left < right)
        {
            s[right] = s[left];
            right--;
        }
        s[left] = temp;
    }
    FastSort(s,start,left - 1,funcptr);
    FastSort(s,right + 1,end,funcptr);
}

void SortStudent(teach *t,bool (*funcptr)(Stu *,Stu *))
{
    FastSort(t->student,0,t->count - 1,funcptr);
}


int main()
{
    //定义结构体变量
    //相当于int stu; 两个单词当成数据类型(struct stdudent) + 变量名stu
    //struct student stu = {2,"lisi",21};
    //赋值
    // stu.id = 1;
    // strcpy(stu.name,"zhangsan");
    // stu.age = 20;


    // struct student stu;
    // InitStu(&stu,1,"zhangsan",20);
    // PrintStu(&stu);

    teach t;
    InitTeach(&t,"李老师",40);
    AddStudent(&t,3,"zhangsan",20);
    AddStudent(&t,1,"lisi",19);
    AddStudent(&t,2,"wangwu",18);
    AddStudent(&t,4,"xiaoming",22);

    SortStudent(&t,CheckName);
    PrintTeach(&t);

    

    return 0;
}